package app.beit.fintrack.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import app.beit.fintrack.bean.NavigationBean;
import app.beit.fintrack.controller.bean.SummaryTrackSessionBean;
import app.beit.fintrack.utils.constr.SessionConstr;

@Controller
@RequestMapping("/")
public class MainController extends BaseController {

	private final Logger log = Logger.getLogger(this.getClass());
	
	@RequestMapping
	public String index(final HttpServletRequest request, final @RequestParam(value="p", required = false) String p){
		this.log.info("forward to main page");
		SummaryTrackSessionBean stsb = (SummaryTrackSessionBean) super.getAttributeInSession(request, SessionConstr.SESSION_SUMMARY);
		stsb = stsb == null ? new SummaryTrackSessionBean() : stsb;
		final List<NavigationBean> navis = new ArrayList<NavigationBean>();
		final NavigationBean bean = new NavigationBean();
		bean.setSort(1);
		bean.setName("สรุปสถานะผลการเบิกจ่าย");
		bean.setUrl(request.getContextPath()+"?p=1");
		navis.add(bean);
		
		if(p != null && p.length() > 0){
			stsb.setCurrentPage(Integer.parseInt(p));
			stsb.setUrl("");
		}
		
		super.setAttributeInSession(request, SessionConstr.SESSION_SUMMARY, stsb);
		super.setAttributeInSession(request, SessionConstr.NAVIGATION, navis);
		//removeSession(request);
		this.log.info("forward : summaryTrackStation/summaryTrackStation");
		return "summaryTrackStation/summaryTrackStation";
	}
	
	public void removeSession(final HttpServletRequest request){
		log.info("remove session SESSION_SUMMARY_PID's value");
		request.getSession().removeAttribute(SessionConstr.SESSION_SUMMARY_PID);
	}
	
}
